package com.hjh.thymeleafdemo.web;

import com.hjh.thymeleafdemo.entity.User;
import com.hjh.thymeleafdemo.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {

    @Resource
    private IUserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id")Long usrId, Model model){
        User user = userService.getUser(usrId);
        model.addAttribute("user",user);
        return "demo/hello";
    }

    @GetMapping(value = "/string")
    public String getString(Model model, HttpServletRequest request){
        model.addAttribute("userName","海孤儿");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("date",new Date());
        return "demo/string";
    }

    @GetMapping(value = "/inline")
    public String inline(Model model){
        model.addAttribute("usrName","海孤儿");
        return "demo/inline";
    }

    @GetMapping(value = "/list")
    public String list(Model model){
        List<User> list = userService.findAllUsers();
        model.addAttribute("users",list);
        return "demo/list";
    }
}
